package com.nulldev.util.web.YouTubeExtractor.testing.tests;

import java.util.concurrent.TimeUnit;

import com.nulldev.util.JVM.JVM;
import com.nulldev.util.concurrency.threadIt.v4.emapi.ExecutorManager;
import com.nulldev.util.internal.testing.Test;
import com.nulldev.util.internal.testing.TestResult;
import com.nulldev.util.web.YouTubeExtractor.YouTubeExtractor;
import com.nulldev.util.web.YouTubeExtractor.YoutubeExtractionOptions;
import com.nulldev.util.web.YouTubeExtractor.extractors.IYouTubeExtractor;
import com.nulldev.util.web.YouTubeExtractor.video.ExtractedVideo;

public class ExtractorTests {

	public static class ExtractorSpecTest extends Test<ExtractedVideo> {

		private final String extractorID;
		private final String videoID;

		public ExtractorSpecTest(final String extractorID, final String videoID) {
			this.extractorID = extractorID;
			this.videoID = videoID;
		}

		public ExtractorSpecTest(final String extractorID, final String videoID, final boolean disabled) {
			super(TestResult.DISABLED);
			this.extractorID = extractorID;
			this.videoID = videoID;
		}

		@Override
		public void test() throws Throwable {
			if (this.extractorID == null) {
				JVM.println("=> Invalid extractor ID!");
				this.fail();
				return;
			}
			if (this.videoID == null) {
				JVM.println("=> Invalid video ID!");
				this.fail();
				return;
			}
			final IYouTubeExtractor iex = YouTubeExtractor.get(this.extractorID);
			if (iex == null) {
				JVM.println("=> Extractor not found: " + this.extractorID);
				this.fail();
				return;
			}
			final ExtractedVideo ev = ExecutorManager.get().queueTask(iex.generateTask(this.videoID, YoutubeExtractionOptions.NO_FALLBACK)).waitFor(30,
					TimeUnit.SECONDS);
			if (ev == null) {
				JVM.println("=> ExtractedVideo was null!");
				this.fail(ev);
				return;
			} else if (!ev.getResult().successful()) {
				this.fail(ev);
			} else {
				this.finish(ev);
			}
		}

	}
}
